using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ctsu.Shared
{
	public class BarScan
	{
		/* the scanner is programmed with a prefix of 2 pound signs(##) and
		 * a suffix of the tilde(~).
		 * the forms keyDown event is listening for 2 consecutive ##.  
		 * 2 consecutive ## turns on a flag to listen for a ~.  This
		 * signals the end of the scan.  This will fire the callback function where
		 * the client can take action
		*/
		public delegate void BarScanCompleteHandler(object sender, BarScanEventArgs e);
		public event BarScanCompleteHandler BarScanComplete;
		
		private bool _isScan = false;  
		private bool _isShift = false;
		private int _poundCount = 0;
		BarScanCompleteHandler _handler;
		
		private Form _form;
		private TextBox _textBox;
		private int _keycount = 0;
        
		public BarScan(Form form, TextBox textBox, BarScanCompleteHandler handler)
		{
			this._form = form;
			this._textBox = textBox;
			this._handler = handler;

			//make sure keyPreview is true
			if(this._form.KeyPreview == false)
				this._form.KeyPreview = true;

			this._form.KeyDown +=new KeyEventHandler(_form_KeyDown);
			this._textBox.Leave +=new EventHandler(_textBox_Leave);
			
		}

		private void _textBox_Leave(object sender, System.EventArgs e)
		{
			this._isScan = false;
			this._poundCount = 0;
			_isShift = false;
		}

		private void _form_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			++_keycount;
			Console.WriteLine("KeyCode: " + e.KeyCode + " ;KeyCount: " + _keycount); 
												
			//if scanning - looking for a enter key
			if(_isShift && e.KeyCode == Keys.Oemtilde && this._isScan)// && this._form.ActiveControl == this._textBox)		
			{
				int id;
				this._isScan = false;
				this._poundCount = 0;
				_isShift = false;

				if(_textBox.Text.Length > 0)
					id = Convert.ToInt32(this._textBox.Text);
				else
					id = -1;
					
				if(this._handler != null)
					this._handler(this, new BarScanEventArgs(id));
				if(BarScanComplete != null)
					BarScanComplete(this, new BarScanEventArgs(id));
			}

			//looking for ## prefix to indicate scanning
			//the # char comes thru in two key strokes -
			//a shift key then a numeric 3
			if(e.KeyCode == Keys.ShiftKey)
			{
				//++_keycount;
				Console.WriteLine("Shift, keyCount: " + _keycount); 
				//set flag to true so we can listen to the next keystroke
				_isShift = true;
			}
			else
			{
				//++_keycount;
				//Console.WriteLine("hift, keyCount: " + _keycount); 
				//if previous key was a shift
				if(this._isShift)
				{
					//set flag to false
					_isShift = false;
					//now we are looking for the 3(#) key
					if(e.KeyCode == Keys.D3)
						++_poundCount;
				
					if(this._poundCount == 2)
					{
						this._poundCount = 0;
						this._isScan = true;
						_textBox.Text = "";
						_textBox.Focus();
					}
				}
				else
				{
					if(this._poundCount == 1)					
						this._poundCount = 0;
				}				
			}
		}

	}

	public class BarScanEventArgs : EventArgs
	{
		public int ID;
		public BarScanEventArgs(int id)
		{
			this.ID = id;
		}
	}
}
